package com.example.demo.rabbit;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * 消息发送服务
 *
 * @author majie
 */
@Service
public class Product {

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 测试广播模式.
     *
     * @param object   消息
     * @param exchange 交换机
     */
    public void broadcast(Object object, String exchange) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(exchange, "", object, correlationData);
    }


    /**
     * direct直连模式
     *
     * @param object     消息
     * @param exchange   交换机
     * @param routingKey 路由键
     */
    public void direct(Object object, String exchange, String routingKey) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(exchange, routingKey, object, correlationData);
    }

}